﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ImageCompose
{
    public partial class Form1 : Form
    {
        List<Image> images = new List<Image>();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            var openFileDig = new OpenFileDialog();
            openFileDig.Multiselect = true;
            if (openFileDig.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                openFileDig.FileNames.ToList().ForEach((x) => {
                    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(x)))
                    {
                        var bmp = Image.FromStream(ms);
                        images.Add((Bitmap)bmp.Clone());
                        flowLayoutPanelContent.Controls.Add(new PictureBox
                        {
                            Image = bmp,
                            Size = bmp.Size
                        });
                    }
                    
                });
            }
        }

        private void btnCompeosed_Click(object sender, EventArgs e)
        {
            var bmp = new Bitmap(images.Count * images[0].Width, images[0].Height);
         
            using (Graphics g = Graphics.FromImage(bmp))
            {
                for (int i = 0; i < images.Count; i++)
                {
                    //g.DrawImage(images[i], new Point(i * images[i].Width, 0)); 此重载方法有误，DrawImage(Image,point) 会自动进行缩放
                    g.DrawImage(images[i], new Rectangle(i * images[i].Width, 0, images[i].Width, images[0].Height));
                }
            }
            bmp.Save("合成图片.png");
            MessageBox.Show("Compelte!");
            System.Diagnostics.Process.Start("explorer.exe", Environment.CurrentDirectory);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            images.Clear();
            flowLayoutPanelContent.Controls.Clear();
        }
    }
}
